// lib_deps =
// 	esphome/ESP32-audioI2S
// 	adafruit/Adafruit NeoPixel@^1.12.3

#include "Arduino.h"
#include "Audio.h"
#include "WiFiMulti.h"
#include <Adafruit_NeoPixel.h>

// Digital I/O used
#define I2S_DOUT 38
#define I2S_BCLK 41
#define I2S_LRC 42
#define PIN 2
#define NUM_LEDS 1

Audio audio;
WiFiMulti wifiMulti;

Adafruit_NeoPixel strip(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ800);

String ssid = "Yeharold";
String password = "123456789";

const char *songs[] = {
    "2141713170", "2625428162", "2058688293", "1958354765", "1804320463",
    "2092377826", "1386673049", "1471039968", "1984888642", "1470852639",
};

void setup()
{
    Serial.begin(115200);
    strip.begin();
    strip.setPixelColor(0, strip.Color(random(0, 255), random(0, 255), random(0, 255)));
    strip.setBrightness(30);
    strip.show();
    WiFi.mode(WIFI_STA);
    wifiMulti.addAP(ssid.c_str(), password.c_str());
    wifiMulti.run();
    if (WiFi.status() != WL_CONNECTED)
    {
        WiFi.disconnect(true);
        wifiMulti.run();
        Serial.print(".");
    }
    audio.setPinout(I2S_BCLK, I2S_LRC, I2S_DOUT);
    audio.setVolume(20); // 根据需要调整音量，范围是0到21

    String songId = songs[random(0, 10)]; // 修复可能的数组越界问题
    String url = "https://music.163.com/song/media/outer/url?id=" + songId + ".mp3";
    audio.connecttohost(url.c_str()); // 使用String对象拼接后转换为const char*
}

void loop()
{
    audio.loop();
}